﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;

namespace LprMiddleWare
{
    public class Helper
    {
        private static readonly NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();

        public static IDataLayer ConnectToOracleDb(string server, string user, string password)
        {
            var conn = OracleConnectionProvider.GetConnectionString(
                server, user, password);
            var dataLayer = XpoDefault.GetDataLayer(conn, AutoCreateOption.SchemaAlreadyExists);
            return dataLayer;
        }

        public static IDataLayer ConnectToDbUntilSuccess(string server, string user, string password)
        {
            var logged = false;
            while (true)
            {
                try
                {
                    var dataLayer = ConnectToOracleDb(server, user, password);
                    return dataLayer;
                }
                catch (Exception ex)
                {
                    if (!logged)
                    {
                        _logger.ErrorException("连接到数据库 " + server + " 出现异常", ex);
                        logged = true;
                    }
                    
                    Thread.Sleep(10000);
                }

            }
        }

        
    }
}
